﻿using Role_el.Models;
using Role_el.Service;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Role_el.Controllers
{
    public class RoleController : ApiController
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="LoginName"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetRoList(int page, int limit, string Name)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var talist = db.Roles.AsEnumerable();
                if (!string.IsNullOrEmpty(Name))
                {
                    talist = talist.Where(p => p.Name.Contains(Name));
                }
                var count = talist.Count();
                var list = talist.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name
                });
                return Json(new
                {
                    count = count,
                    data = list.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList()
                });
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult AddRole([FromBody] Roles ro)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                Roles r = db.Roles.FirstOrDefault(p => p.Name == ro.Name);
                if (r == null)
                {
                    db.Roles.Add(ro);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new
                        {
                            code = 0,
                            msg = "添加成功！"
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            code = -1,
                            msg = "添加失败！"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        code = -2,
                        msg = "该角色已存在！"
                    });
                }
                
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult EditRole([FromBody] Roles ro)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                Roles rolist = db.Roles.SingleOrDefault(p => p.Id == ro.Id);
                rolist.Name = ro.Name;
                Roles r = db.Roles.FirstOrDefault(p => p.Name == ro.Name);
                if (r == null)
                {
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new
                        {
                            code = 0,
                            msg = "修改成功！"
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            code = -1,
                            msg = "修改失败！"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        code = -2,
                        msg = "该角色已存在！"
                    });
                }
                
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult DelRole([FromBody] Roles ro)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var r = db.UserRoles.FirstOrDefault(p => p.Roled == ro.Id);
                if (r == null)
                {
                    var rr = db.RoleModules.FirstOrDefault(p => p.RoleId == ro.Id);
                    if (rr == null)
                    {
                        db.Entry<Roles>(ro).State = EntityState.Deleted;
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "删除成功！" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "删除失败！" });
                        }
                    }
                    else
                    {
                        return Json(new { code = -2, msg = "该角色拥有管理模块！" });
                    }
                }
                else
                {
                    return Json(new { code = 3, msg = "该角色正在被使用！" });
                }
            }
        }
        /// <summary>
        /// 显示全部模块
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAllModules()
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var datalist = db.Modules.Where(p => p.ParentId == 0).ToList();
                var bb = datalist.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).ToList();
                List<Module> data = new List<Module>();
                foreach (var item in bb)
                {
                    var list = db.Modules.Where(p => p.ParentId == item.Id).ToList();
                    var cc = list.Select(p => new Modules()
                    {
                        Id = p.Id,
                        Name = p.Name,
                        ParentId = p.ParentId,
                        Path = p.Path,
                        Weight = p.Weight,
                    }).ToList();
                    var aa = new Module()
                    {
                        Id = item.Id,
                        Name = item.Name,
                        ParentId = item.ParentId,
                        Path = item.Path,
                        Weight = item.Weight,
                        children = cc
                    };
                    data.Add(aa);
                };
                return Json(new { data = data, code = 0 });
            }
        }
        /// <summary>
        /// 显示我的模块
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetMyModules([FromBody]Roles role)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var datalist = db.RoleModules.Where(p => p.RoleId == role.Id).ToList();
                var list = new List<int>();
                foreach (var item in datalist)
                {
                    list.Add(item.ModuleId);
                }
                return Json(new { data = list, code = 0 });
            }
        }
        /// <summary>
        /// 分配
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult EditRM(string Id, string Value)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                int n = 0;
                int id = Convert.ToInt32(Id);
                if (Value != null)
                {
                    string[] strArray = Value.Split(','); //字符串转数组
                    List<int> abc = new List<int>();
                    foreach (var g in strArray)
                    {
                        abc.Add(Convert.ToInt32(g));
                    }
                    var datalist = db.RoleModules.Where(p => p.RoleId == id).ToList();
                    foreach (var item in datalist)
                    {
                        db.RoleModules.Remove(item);
                        n += db.SaveChanges();
                    }
                    foreach (var i in abc)
                    {
                        var dd = new RoleModules()
                        {
                            RoleId = id,
                            ModuleId = i
                        };
                        db.RoleModules.Add(dd);
                        n += db.SaveChanges();
                    }
                }
                else
                {
                    var datalist = db.RoleModules.Where(p => p.RoleId == id).ToList();
                    foreach (var item in datalist)
                    {
                        db.RoleModules.Remove(item);
                        n += db.SaveChanges();
                    }
                }
                if (n >= 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }

    }
}
